//
//  QSProblemDataViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/22.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSProblemDataViewController.h"
#import "QSWaitingForProcessedCell.h"
#import "QSSubmitProcessingViewController.h"
#import "QSProblemProcessingInterface.h"
#import "QSProblemDetailViewController.h"
#import "QSSelectDateView.h"
@interface QSProblemDataViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic, assign)NSInteger state;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)NSString *month;
@property (nonatomic, strong)QSSelectDateView *selectDateView;
@end
static NSString *waitingForProcessedCell = @"WaitingForProcessedCell";
@implementation QSProblemDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addRefreshHeader];
    [self addRefreshFooter];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    if (_index == 0) {
        self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavgationBarHeight - kRealValue(54));
    }else{
        self.selectDateView = [[QSSelectDateView alloc]initWithFrame:CGRectMake(10, 10, kScreenWidth - 20, kRealValue(40))];
        self.selectDateView.layer.cornerRadius = 5;
        [self.view addSubview:self.selectDateView];
        self.tableView.frame = CGRectMake(0, 50, kScreenWidth, kScreenHeight - kNavgationBarHeight- kRealValue(50) - kRealValue(78));
        WeakSelf(weakSelf);
        self.selectDateView.didSelectDateFinishBlock = ^(NSString * _Nonnull date) {
            weakSelf.month = date;
            [weakSelf tableViewShouldUpdateDataByPageIndex:1];
        };
    }
    [self.tableView registerNib:[UINib nibWithNibName:@"QSWaitingForProcessedCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:waitingForProcessedCell];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    [self startRefreshing];
}


- (void)tableViewShouldUpdateDataByPageIndex:(NSInteger)pageIndex{
    if (_index == 0) {
        self.state = 2;//待处理
        self.month = nil;
    }else{
        self.state = 4;//已处理
        if (!self.month) {
            NSString *curruntDate = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970] *1000];
            self.month = curruntDate;
        }
    }
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
    [QSProblemProcessingInterface getApiProblemProcessingListWithPageIndex:pageIndex State:_state Month:_month andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSProblemDealingListModel * _Nonnull resultModel) {
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            [QSAppKeyWindow hideHud];
            if (pageIndex == 1) {
                [self.dataArray removeAllObjects];
            }
            [self.dataArray addObjectsFromArray:resultModel.list];
            self.tableView.emptyDataSetSource = self;
            self.tableView.emptyDataSetDelegate = self;
            [self.tableView reloadData];
            [self.tableView qs_endRefreshing];
            if (pageIndex == resultModel.pages) {
//                self.tableView.mj_footer.hidden = YES;
                [self endRefreshingWithNoMoreData];
            }else{
                self.tableView.mj_footer.hidden = NO;
            }
            if (self->_index == 0) {
                NSLog(@"待我处理%ld个", self.dataArr.count);
            }
        }
    }];
    
}

- (NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

#pragma mark - **************** UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
//    if (_state == 1) {
//        return 2;
//    }else{
//    return 4;
//    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    QSWaitingForProcessedCell *cell = [tableView dequeueReusableCellWithIdentifier:waitingForProcessedCell forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kRealValue(95 + 10);//cell间距
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    QSProblemDeaingModel *model = self.dataArray[indexPath.row];
    if (_state == 2) {
        //待处理展示处理页
        QSSubmitProcessingViewController *vc = [QSSubmitProcessingViewController new];
        vc.state = self.state;
        vc.problemId = model.id.copy;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        //已处理展示问题详情
        QSProblemDetailViewController *vc = [QSProblemDetailViewController new];
        vc.problemId = model.id.copy;
        [self.navigationController pushViewController:vc animated:YES];
    }
}



#pragma mark - DZNEmptyDataSetSource
/**
 *  返回标题文字
 */
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = @"暂无需要处理的问题!";
    NSDictionary *attributes = @{ NSFontAttributeName: [UIFont boldSystemFontOfSize:14.0f],
                                     NSForegroundColorAttributeName: [UIColor QSColorBlack666666] };
    return [[NSAttributedString alloc]initWithString:text attributes:attributes];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    scrollView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    return [UIImage imageNamed:@"ic_problem_null"];
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (self.tableView) {
        [self tableViewShouldUpdateDataByPageIndex:1];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
